package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xmy
 * @version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    public List<Setmeal> getByCategoryId(Long categoryId) {
        List<Setmeal> list=setmealMapper.GetByCategoryId(categoryId);
        return list;
    }

    /**
     * 根据套餐id查询对应菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getListBySetmealId(Long id) {
        return setmealMapper.getListBySetmealId(id);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        PageResult pageResult=new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getById1(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    //TODO 写到前端时完善一下该2方法
    public void update(SetmealDTO setmealDTO) {
//        Setmeal setmeal=new Setmeal();
//        BeanUtils.copyProperties(setmealDTO,setmeal);
//        Long setmealId=setmealDTO.getId();
//        setmealMapper.update(setmeal);
//        //删除该套餐下的原有菜品
//        setmealDishMapper.delete(setmealDTO.getId());
//
//
//        //插入新菜品
//        List<SetmealDish> setmealDishList=setmealDTO.getSetmealDishes();
//        if(setmealDishList!=null&&setmealDishList.size()>0){
//            setmealDishList.forEach(setmealDish -> {
//                setmealDish.setSetmealId(setmealId);
//            });
//            setmealDishMapper.insertBatch(setmealDishList);
//        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        System.out.println("套餐1"+setmeal+setmealDTO);
        //1、修改套餐表，执行update
        setmealMapper.update(setmeal);

        //套餐id
        Long setmealId = setmealDTO.getId();

        //2、删除套餐和菜品的关联关系，操作setmeal_dish表，执行delete
        setmealDishMapper.delete(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //3、重新插入套餐和菜品的关联关系，操作setmeal_dish表，执行insert
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 菜品起售。停售
     * @param status
     * @param id
     */
    //
    public void updateStatus(Integer status, Integer id) {
        if(status==0){
            setmealMapper.updateStatus(status,id);
        }else {
            //判断套餐中是否存在未起售菜品
            List<Long> ids=setmealDishMapper.getDishIds(id);//获取菜品id
            for(Long id1:ids){
                Dish dish=dishMapper.getById(id1);
                if(dish.getStatus()==StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
            setmealMapper.updateStatus(status,id);
        }


    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO ,setmeal);

        //向套餐表插入数据
        setmealMapper.insert(setmeal);
        //获取生成的套餐id
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        //保存套餐和菜品的关系
        setmealDishMapper.insertBatch(setmealDishes);


    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        //判断当前套餐是否能够删除--是否存在起售中的套餐

        for (Long id : ids) {
            SetmealVO setmealVO=setmealMapper.getById(id);
            if(setmealVO.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //删除对应套餐中的菜品
        for (Long id:ids){
            setmealDishMapper.delete(id);
        }

        //删除套餐
        setmealMapper.deleteBatch(ids);
    }
}
